using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

// /optimize+ /define:X;Y /unsafe /checked+ /nostdlib-

namespace ImmediateWindow
{
	public partial class OptionsForm : Form
	{
		CompilerKnobs theKnobs;
		Control[] font_controls;
		public OptionsForm(CompilerKnobs knobs,Control[] fonted)
		{
			theKnobs = knobs;
			font_controls=fonted;
			InitializeComponent();
		}

		private void CSKnobForm_Load(object sender, EventArgs e)
		{
			RadioButton language = LanguageGroupBox.Controls[theKnobs.Language.ToString()] as RadioButton;
			language.Checked = true;
			WarnLevelComboBox.SelectedIndex = theKnobs.WarningLevel;
			WarnAsErrorCheckBox.Checked = theKnobs.WarningsAsErrors;
			string define="/define:";
			if (theKnobs.Switches.Contains(define))
			{
				int startIndex = theKnobs.Switches.IndexOf(define);
				string switches = string.Concat(theKnobs.Switches, " ");
				int endIndex = switches.IndexOf(' ', startIndex);
				startIndex += define.Length;
				SymbolTextBox.Text = theKnobs.Switches.Substring(startIndex, endIndex-startIndex);
			}
			UnsafeCheckBox.Checked = theKnobs.Switches.Contains("/unsafe");
			OptimizeCheckBox.Checked = theKnobs.Switches.Contains("/optimize+");
			CheckCheckBox.Checked = theKnobs.Switches.Contains("/checked+");
			mscorlibCheckBox.Checked = theKnobs.Switches.Contains("/nostdlib+");
			AddedTextBox.Text = theKnobs.AddedSwitches;
			AppTypeComboBox.SelectedIndex = (int)theKnobs.ApplicationType;
			if(font_controls.Length>0)
			{
				FontLabel.Text = font_controls[0].Font.ToString();
				FontLabel.ForeColor = font_controls[0].ForeColor;
//				FontLabel.Font = font_controls[0].Font;
			}
			else
				FontLabel.Text="";
		}
		private void ApplyButton_Click(object sender, EventArgs e)
		{
			foreach (RadioButton lang in LanguageGroupBox.Controls)
				if (lang.Checked)
				{
					theKnobs.Language = (DesignNotation)Enum.Parse(typeof(DesignNotation), lang.Name);
					break;
				}
			theKnobs.WarningLevel = int.Parse(WarnLevelComboBox.SelectedIndex.ToString());
			theKnobs.WarningsAsErrors = WarnAsErrorCheckBox.Checked;
			StringBuilder switches=new StringBuilder();
			string symbols=SymbolTextBox.Text.Trim();
			if (symbols.Length > 0)
				switches.AppendFormat("/define:{0} ",symbols);
			if (theKnobs.Language == DesignNotation.CSharp)
			{
				if (UnsafeCheckBox.Checked)
					switches.Append("/unsafe ");
				switches.AppendFormat("/optimize{0} ", OptimizeCheckBox.Checked ? "+" : "-");
				switches.AppendFormat("/checked{0} ", CheckCheckBox.Checked ? "+" : "-");
				switches.AppendFormat("/nostdlib{0} ", mscorlibCheckBox.Checked ? "+" : "-");
			}
			if(theKnobs.Language==DesignNotation.JavaScript)
				switches.Append("/print+ ");

			theKnobs.Switches = switches.ToString();
      theKnobs.AddedSwitches = AddedTextBox.Text.Trim();
			theKnobs.ApplicationType=(ApplicationType)AppTypeComboBox.SelectedIndex;
		}

		private void FontButton_Click(object sender, EventArgs e)
		{
			TheFontDialog.ShowColor=true;
			if (font_controls.Length <= 0) return;
			Font font = font_controls[0].Font;
			TheFontDialog.Font = font;
			TheFontDialog.Color = font_controls[0].ForeColor;

			if (TheFontDialog.ShowDialog() != DialogResult.OK) return;
			foreach (Control ctrl in font_controls)
			{
				ctrl.Font = TheFontDialog.Font;
				ctrl.ForeColor = TheFontDialog.Color;
			}
			FontLabel.Text = TheFontDialog.Font.ToString();
			FontLabel.ForeColor = TheFontDialog.Color;
//			FontLabel.Font = TheFontDialog.Font;
		}
	}
}